import request from '/@/utils/http/axios';
import { useUserStore } from '/@/store';

// 定义课程状态类型
export type CourseStatus = 'PENDING' | 'PROGRESS' | 'COMPLETED' | 'CANCELED'; 

// 定义课程数据类型
interface Course {
  id: number;
  title: string;
  cover: string;
  teacherName: string;
  startTime: string;
  endTime: string;
  mobile: string;
  status: 'PENDING' | 'PROGRESS' | 'COMPLETED' | 'CANCELED';
  studentConfirmed: boolean;
  teacherConfirmed: boolean;
  hasEvaluation?: boolean;
  hoursPerDay: number;    // 每天课时数
  days: number;           // 总天数
  totalAmount: number;    // 总金额
  studentUsername: string;
  studentMobile: string;
}

// 获取我的课程列表（支持按状态过滤）
export const listMyCoursesApi = async (userId: number, status?: CourseStatus): Promise<Course[]> => {
  const response = await request<{ data: Course[] }>({
    method: 'get',
    url: 'api/course/my',
    params: { userId, status }, // 传递 userId 和 status
  });
  return response.data;
};

// 取消课程（需传递 userId）
export const cancelCourseApi = async (id: number, userId: number): Promise<void> => {
  await request({
    method: 'post',
    url: `api/course/${id}/cancel`,
    params: { userId }, // 传递 userId
  });
};

// 确认课程完成（需传递 userId）
export const completeCourseApi = async (id: number, userId: number): Promise<void> => {
  await request({
    method: 'post',
    url: `api/course/${id}/complete`,
    params: { userId }, // 传递 userId
  });
};

// 教师课程查询接口
export const listTeacherCoursesApi = async (
  status?: CourseStatus
): Promise<Course[]> => {
  const userStore = useUserStore(); // 获取用户存储实例
  const userId = userStore.user_id; // 假设用户ID存储在user_id字段
  
  const response = await request<{ data: Course[] }>({
    method: 'get',
    url: 'api/course/teacher',
    params: { 
      userId: userId, // 确保参数名与后端一致
      status: status 
    }
  });
  return response.data;
};

// 状态更新接口
export const updateCourseStatusApi = async (
  courseId: number,
  status: CourseStatus
): Promise<void> => {
  await request({
    method: 'post',
    url: 'api/course/updateStatus',
    params: {  // 使用URL参数传递
      courseId,
      status
    }
  });
};

// 获取未评价课程
export const listUnratedCoursesApi = async (userId: number): Promise<Course[]> => {
  const response = await request<{ data: Course[] }>({
    method: 'get',
    url: 'api/course/unrated',
    params: { userId },
  });
  return response.data;
};

// 提交评价接口
export const submitEvaluationApi = async (
  data: {
    courseId: number;
    score: number;
    comment: string;
  },
  userId: number // userId参数
): Promise<void> => {
  await request({
    method: 'post',
    url: 'api/course/submitEvaluation',
    params: { userId }, // 将userId作为URL参数传递
    data, // 请求体数据保持不变
  });
};